define(['ionic', 'services'], function () {
    angular.module('U9Mobile.services')
        .factory('ExtService', ['UserService', 'ConnectService', 'ContactService', 'UtilService', 'AppConstants',
            function (UserService, ConnectService, ContactService, UtilService, AppConstants) {
                return {
                    publishExternalAPI: publishExternalAPI
                };

                function publishExternalAPI() {
                    var u9 = window.u9 || (window.u9 = {});

                    u9.checkNetwork = function () {
                        return UtilService.checkNetwork();
                    };

                    u9.toU9URL = function (srcURL) {
                        var u9mainAppDir = UtilService.getMainAppDir();
                        return u9mainAppDir + srcURL;
                    };

                    u9.showLoading = function (tips) {
                        UtilService.showLoading(tips);
                    };
                    u9.hideLoading = function () {
                        UtilService.hideLoading();
                    };

                    u9.alert = function (message, title) {
                        return UtilService.alert(message, title);
                    };
                    u9.confirm = function (message, title) {
                        return UtilService.confirm(message, title);
                    };

                    u9.getConnectSetting = function () {
                        return ConnectService.getAll();
                    };
                    u9.getLoginData = function () {
                        return {
                            EntCode: ConnectService.getConnect('company'),
                            OrgId: UserService.getUserParams('org').id,
                            OrgCode: UserService.getUserParams('org').code,
                            UserCode: UserService.getUserParams('account'),
                            token: UserService.getUserToken().access_token,
                            CultureName: 'zh-CN'
                        };
                    };
                    u9.v_u9 = AppConstants.APP_VERSION;
                    u9.registerGetContactFn = function (appId, fn) {
                        ContactService.bindGetContactFn(appId, fn);
                    };
                    u9.callSV = function (jsonData) {
                        var svFullName = jsonData['name'],
                            params = jsonData['params'],
                            url = AppConstants.PROXY_SCRIPT_URL.format({
                                address: ConnectService.getConnect('address'),
                                port: ConnectService.getConnect('port'),
                                portalUrl: 'null',
                                svFullName: svFullName
                            });
                        return require([url], function (bpObj) {
                            params.unshift({
                                EntCode: ConnectService.getConnect('company'),
                                OrgCode: UserService.getUserParams('org').code,
                                UserCode: UserService.getUserParams('account'),
                                CultureName: 'zh-CN'
                            });
                            if (jsonData['onSuccess']) {
                                params.push(jsonData['onSuccess']);
                            } else {
                                params.push(onSuccess);
                            }
                            if (jsonData['onFailure']) {
                                params.push(jsonData['onFailure']);
                            } else {
                                params.push(onFailure);
                            }

                            var svcProxy = AppConstants.PROXY_SVC_URL.format({
                                address: ConnectService.getConnect('address'),
                                port: ConnectService.getConnect('port'),
                                svFullName: svFullName
                            });
                            try {
                                bpObj.set_path(svcProxy);
                                return bpObj.Do.apply(bpObj, params);
                            } catch (e) {
                                return jsonData.onFailure(e);
                            }

                        }, function () {
                            jsonData.onFailure();
                        });
                    };

                    u9.plugins = {};
                    if (window.cordova) {
                        u9.plugins.barcodeScanner = cordova.plugins.barcodeScanner;
                    }
                    if (window.datePicker) {
                        u9.plugins.datePicker = window.datePicker;
                    }
                    if (window.plugins && window.plugins.CallNumber) {
                        u9.plugins.CallNumber = window.plugins.CallNumber;
                    }
                    if (window.sms) {
                        u9.plugins.sms = window.sms;
                    }
                    if (window.cordova && cordova.plugins.email) {
                        u9.plugins.email = cordova.plugins.email;
                    }

                    function onSuccess(data) {
                    }

                    function onFailure(data) {
                    }
                }
            }
        ]);
});